package com.mycat.web.controller.ucenter;

import com.mycat.common.entity.InvokeResult;
import com.mycat.dao.dto.ucenter.CustomLoginDto;
import com.mycat.web.command.ucenter.LoginResponseData;
import com.mycat.web.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author roger.
 * @email luojie@yzworld.cn
 * @date 2016/10/8
 */
@RestController
@RequestMapping
@Api(value = "用户公开相关接口",description = "用户公开相关接口")
public class IndexController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     * 登录
     *
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登陆接口")
    public ResponseEntity<InvokeResult<LoginResponseData>> login(@RequestBody CustomLoginDto customLoginDto) {
        InvokeResult invokeResult = null;
        if (null == customLoginDto || StringUtils.isEmpty(customLoginDto.getUserName()) || StringUtils.isEmpty(customLoginDto.getPassword())){
            invokeResult = InvokeResult.failure("参数错误");
            return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken();
        token.setUsername(customLoginDto.getUserName());
        token.setPassword(customLoginDto.getPassword().toCharArray());
        try {
            subject.logout();
            subject.login(token);
            LoginResponseData responseData = new LoginResponseData();
            responseData.setCustomer(super.getLoginUser());
            responseData.setToken(String.valueOf(subject.getSession().getId()));
            invokeResult = InvokeResult.success(responseData);
        } catch (UnknownAccountException e) {
            logger.error(e.getMessage(), e);
            invokeResult = InvokeResult.failure(e.getMessage());
        } catch (LockedAccountException e) {
            logger.error(e.getMessage(), e);
            invokeResult = InvokeResult.failure(e.getMessage());
        } catch (IncorrectCredentialsException e) {
            logger.error(e.getMessage(), e);
            invokeResult = InvokeResult.failure(e.getMessage());
        } catch (ExcessiveAttemptsException e) {
            logger.error(e.getMessage(), e);
            invokeResult = InvokeResult.failure(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            invokeResult = InvokeResult.error();
        }
        return new ResponseEntity(invokeResult, HttpStatus.valueOf(invokeResult.getCode()));
    }

}
